package com.moge.anjian.utils;

import android.content.Context;
import android.content.SharedPreferences;

import com.moge.anjian.App;

public class SpUtils {
    private static SharedPreferences sp;
    private static SharedPreferences.Editor editor;


    private static SharedPreferences getSp(Context context) {
        if (sp == null) {
            sp = context.getSharedPreferences("SpUtils", Context.MODE_PRIVATE);
            editor = sp.edit();
        }
        return sp;
    }


    /**
     * 存入字符串
     *
     * @param key     字符串的键
     * @param value   字符串的值
     */
    public static void putString(String key, String value) {
        SharedPreferences preferences = getSp(App.get());
        //存入数据
        SharedPreferences.Editor editor = preferences.edit();
        editor.putString(key, value);
        editor.commit();
    }

    /**
     * 获取字符串
     *
     * @param key     字符串的键
     * @return 得到的字符串
     */
    public static String getString(String key) {
        SharedPreferences preferences = getSp(App.get());
        return preferences.getString(key, "");
    }

    public static void remove(Context context, String key) {
        SharedPreferences preferences = getSp(context);
        //存入数据
        SharedPreferences.Editor editor = preferences.edit();
        editor.remove(key);
        editor.commit();
    }
}
